package com.otitan.forest.right.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otitan.forest.right.entity.SysRoleEntity;
import com.otitan.forest.right.entity.SysUserRoleRelationEntity;
import com.otitan.forest.right.mapper.SysUserRoleRelationMapper;
import com.otitan.forest.right.service.SysUserRoleRelationService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("sysUserRoleRelationService")
public class SysUserRoleRelationServiceImpl extends ServiceImpl<SysUserRoleRelationMapper, SysUserRoleRelationEntity> implements SysUserRoleRelationService {


    @Override
    public void insertByUserIdAndRoleId(String userId, List<SysRoleEntity> roleIds) {
        for (SysRoleEntity sysRoleEntity : roleIds) {
            SysUserRoleRelationEntity sysUserRoleRelationEntity = new SysUserRoleRelationEntity(userId, sysRoleEntity.getId());
            save(sysUserRoleRelationEntity);
        }
    }

    @Override
    public void updateByUserIdAndRoleId(String userId, List<SysRoleEntity> roleIds) {
        for (SysRoleEntity sysRoleEntity : roleIds) {
            SysUserRoleRelationEntity sysUserRoleRelationEntity = new SysUserRoleRelationEntity(userId, sysRoleEntity.getId());
            LambdaQueryWrapper<SysUserRoleRelationEntity> queryWrapper = Wrappers.lambdaQuery();
            //查询条件示例
            queryWrapper.eq(SysUserRoleRelationEntity::getUserId, userId);
            SysUserRoleRelationEntity sysUserRoleRelation = getOne(queryWrapper);
            if (sysUserRoleRelation == null) {
                save(sysUserRoleRelationEntity);
            } else {
                updateById(sysUserRoleRelationEntity);
            }

        }
    }
}